package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentMineExchangeBinding;
import com.ptszyxx.popose.module.main.mine.adapter.MineExchangeAdapter;
import com.ptszyxx.popose.module.main.mine.vm.MineExchangeVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.exchange.ExchangeEntity;
import com.ysg.utils.YDialogUtil;

import java.util.List;

/**
 * 金币兑换钻石
 */
public class MineExchangeFragment extends BaseFragment<FragmentMineExchangeBinding, MineExchangeVM> {
    private MineExchangeAdapter adapter;

    public static MineExchangeFragment getInstance() {
        return new MineExchangeFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_exchange;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineExchangeVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineExchangeVM.class);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new MineExchangeAdapter(viewModel);
        binding.refreshView.initAdapterGrid(adapter, 2);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestList(true);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, o -> binding.refreshView.setAdapterUI(adapter, (List<ExchangeEntity>) o, true));
        viewModel.uc.onExchangeDialogEvent.observe(this, o -> showDialogExchange((ExchangeEntity)o));
    }

    private void showDialogExchange(ExchangeEntity entity) {
        String content = String.format(getResources().getString(R.string.mine_exchange_dialog_content), entity.getGoldCoinStr(), entity.getDiamondsStr());
        YDialogUtil.getInstance().showConfirm(getContext(), content).setOnConfirmListener(()->{
            viewModel.requestExchange(entity);
        });
    }


}
